$(function () {
    var getId=GetRequest();
    var meetid = getId['meet_id'];
    var openid = getId['openid'];
    var memberid = getId['member_id'];
    var tip = getId['title'];
    var overtime = getId['overtime'];
    function GetRequest() {
        var url = location.search; //获取url中"?"符后的字串
        var theRequest = new Object();
        if (url.indexOf("?") != -1) {
            var str = url.substr(1);
            strs = str.split("?");
            for (var i = 0; i < strs.length; i++) {
                theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
    var newtime =Date.parse(new Date());
    var oldtime =Date.parse(overtime);
    if(newtime > oldtime){
        alert('本次评分已结束');
        window.location.href = 'http://slave.mtaction.com/mtshare-web/timeout.html';
    }else {
        if (meetid) {
            localStorage.setItem('meet', meetid);
            localStorage.setItem('memberid', memberid);
            localStorage.setItem('tip', tip);
        }
        if (!openid) {
            window.location.href = 'http://api.mtaction.com/dian/Qm/carve/auto.php?url=http://api.mtaction.com/dian/Qm/carve/getopenscore.php';
        }else{
            $('.content').show();
        }
            window.formData = {
                openid: openid,
                q_one: null,
                q_two: null,
                q_three: null,
                q_four: null,
                q_five: null,
                to_member: null,
                to_meet: null,
            };

        $.get('http://slave.mtaction.com/api/members',
            function (data) {
                for (var i = 0; i < data.length; i++) {
                    if (window.formData.openid == data[i].openid) {
                        if(data[i].state!=1) {
                            alert('您尚处于审核状态,暂不能进行评分操作!');
                            openid = 1;
                            window.location.href = 'http://slave.mtaction.com/mtshare-web/again.html';
                        }
                    }
                }
            });
        $.get('http://slave.mtaction.com/api/scores',
            function (data) {
                for (var i = 0; i < data.length; i++) {
                    if (localStorage.getItem('meet') == data[i].meet_id && openid ==data[i].openid) {
                        alert('您已经提交了评分，请不要重复提交!');
                        openid = 1;
                        window.location.href = 'http://slave.mtaction.com/mtshare-web/again.html';
                    }
                }
            });
        $.get('http://slave.mtaction.com/api/members',
            function (data) {
                for (var i = 0; i < data.length; i++) {
                    if (localStorage.getItem('memberid') == data[i].id) {
                        console.log(data[i].name);
                        $('.member').append(data[i].name);
                    }
                }
            });
        $('.biaoti').append(localStorage.getItem('tip'));
        $('#question_one').change(function () {
            var myrulue = /^([0-9]|[1][0-9]|20)$/;
            var q_one = $(this).val();
            if (!myrulue.test(q_one)) {
                $('.one').css('display', 'block');
                $('.one').text('请输入分数(0-20)');
            } else {
                $('.one').css('display', 'none');
                window.formData.q_one = q_one;
            }
        });
        $('#question_two').change(function () {
            var myrulue = /^([0-9]|[1][0-9]|20)$/;
            var q_two = $(this).val();
            if (!myrulue.test(q_two)) {
                $('.two').css('display', 'block');
                $('.two').text('请输入分数(0-20)');
            } else {
                $('.two').css('display', 'none');
                window.formData.q_two = q_two;
            }
        });
        $('#question_three').change(function () {
            var myrulue = /^([0-9]|[1][0-9]|20)$/;
            var q_three = $(this).val();
            if (!myrulue.test(q_three)) {
                $('.three').css('display', 'block');
                $('.three').text('请输入分数(0-20)');
            } else {
                $('.three').css('display', 'none');
                window.formData.q_three = q_three;
            }
        });
        $('#question_four').change(function () {
            var myrulue = /^([0-9]|[1][0-9]|20)$/;
            var q_four = $(this).val();
            if (!myrulue.test(q_four)) {
                $('.four').css('display', 'block');
                $('.four').text('请输入分数(0-20)');
            } else {
                $('.four').css('display', 'none');
                window.formData.q_four = q_four;
            }
        });
        $('#question_five').change(function () {
            var myrulue = /^([0-9]|[1][0-9]|20)$/;
            var q_five = $(this).val();
            if (!myrulue.test(q_five)) {
                $('.five').css('display', 'block');
                $('.five').text('请输入分数(0-20)');
            } else {
                $('.five').css('display', 'none');
                window.formData.q_five = q_five;
            }
        });
        $('#advice_member').change(function () {
            var to_member = $(this).val();
            window.formData.to_member = to_member;
        });
        $('#advice_meet').change(function () {
            var to_meet = $(this).val();
            window.formData.to_meet = to_meet;
        });

        $(function () {
            $('#tijiao').click(function () {
                var meet_id = localStorage.getItem('meet');
                var question_one = window.formData.q_one;
                var question_two = window.formData.q_two;
                var question_three = window.formData.q_three;
                var question_four = window.formData.q_four;
                var question_five = window.formData.q_five;
                var advice_member = window.formData.to_member;
                var advice_meet = window.formData.to_meet;
                var sum = Number(question_one) + Number(question_two) + Number(question_three) + Number(question_four) + Number(question_five);
                var openid = window.formData.openid;
                if(question_one==null){
                    alert('问题一尚未评分');
                }
                if(question_two==null){
                    alert('问题二尚未评分');
                }
                if(question_three==null){
                    alert('问题三尚未评分');
                }
                if(question_four==null){
                    alert('问题四尚未评分');
                }
                if(question_five==null){
                    alert('问题五尚未评分');
                }
                console.log(advice_meet);
                $.post("http://slave.mtaction.com/api/scores/create", {//注册接口
                    meet_id: meet_id,
                    question_one: question_one,
                    question_two: question_two,
                    question_three: question_three,
                    question_four: question_four,
                    question_five: question_five,
                    advice_member: advice_member,
                    advice_meet: advice_meet,
                    sum: sum,
                    openid: openid,
                }, function (data) {
                    if (data.error) {
                        alert(data.error);
                    } else {
                        alert("您提交的总分为:"+sum);
                        localStorage.removeItem("meet");
                        localStorage.removeItem("memberid");
                        localStorage.removeItem("tip");
                        window.location.href = 'http://slave.mtaction.com/mtshare-web/success.html';
                    }
                });
            });

        });
    }
});

